<template>
  <div class="form-single-fragment" style="position: relative;">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" size="mini" label-position="right">
      <el-row :gutter="20" class="form-single-info">
        <el-col :span="20">
          <el-col :span="12">
            <el-form-item label="企业名称" prop="enterpriseName">
              <el-input class="input-item" v-model="ruleForm.enterpriseName" placeholder="请输入" maxlength="20"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位属性" prop="unitAttribute">
              <el-select v-model="ruleForm.unitAttribute" placeholder="请选择">
                <el-option label="国有企业" value="1"></el-option>
                <el-option label="国有控股企业" value="2"></el-option>
                <el-option label="外资企业" value="3"></el-option>
                <el-option label="合资企业" value="4"></el-option>
                <el-option label="私营企业" value="5"></el-option>
                <el-option label="事业单位" value="6"></el-option>
                <el-option label="国家行政机关" value="7"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所在地区" prop="districtCode">
              <el-cascader v-model="ruleForm.districtCode"
                ref="ref_areaCodes"
                :options="areaCodelist"
                :props="areaCodeprops"
                :show-all-levels="false"
                clearable
                @change="areaCodesChange"></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="资质等级" prop="qualificationLevel">
              <el-input class="input-item" v-model="ruleForm.qualificationLevel" placeholder="请输入" maxlength="20"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="负责人" prop="manager">
              <el-input class="input-item" v-model="ruleForm.manager" placeholder="请输入" maxlength="20"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="职务" prop="custPost">
              <el-input class="input-item" v-model="ruleForm.custPost" placeholder="请输入" maxlength="20"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号" prop="telephone">
              <el-input class="input-item" v-model="ruleForm.telephone" placeholder="请输入" maxlength="11" type='tel' onkeyup="value=value.replace(/[^\d]/g,'')"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="我方对接人" prop="ourContacts">
              <el-input class="input-item" v-model="ruleForm.ourContacts" placeholder="请输入" maxlength="20"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="业务范畴" prop="businessScope">
              <el-input type="textarea" class="input-item" v-model="ruleForm.businessScope" placeholder="请输入" maxlength="100" rows="3"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="企业简介" prop="enterpriseProfile">
              <el-input type="textarea" class="input-item" v-model="ruleForm.enterpriseProfile" placeholder="请输入" maxlength="100" rows="3"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item style="padding: 20px 0;">
              <el-button size="mini" @click="onCancel(false)">取消</el-button>
              <el-button type="primary" size="mini" @click="submitForm()">保存</el-button>
            </el-form-item>
          </el-col>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { EcologicalkKnowledge } from '@/api/knowledgeBases.js';
export default {
  name: 'addknowledge',
  props: {
    EcologicalId: {
      default: undefined
    }
  },
  data () {
    var checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请输入'))
      } else {
        const reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/
        if (reg.test(value)) {
          callback()
        } else {
          return callback(new Error('请输入正确的手机号'))
        }
      }
    }
    return {
      isShowloading: false,
      areaCodelist: [],
      areaCodes: [],
      areaCodeprops: {
        label: 'name',
        value: 'id',
        children: 'children',
        checkStrictly: true,
        emitPath: false
      },
      ruleForm: {
        enterpriseName: '',
        unitAttribute: '',
        districtCode: '',
        qualificationLevel: '',
        manager: '',
        custPost: '',
        telephone: '',
        ourContacts: '',
        businessScope: '',
        enterpriseProfile: ''
      },
      rules: {
        enterpriseName: [{ required: true, message: '请输入', trigger: 'blur' }],
        unitAttribute: [{ required: true, message: '请选择', trigger: 'change' }],
        districtCode: [{ required: true, message: '请选择', trigger: 'change' }],
        qualificationLevel: [{ required: true, message: '请选择', trigger: 'change' }],
        manager: [{ required: true, message: '请输入', trigger: 'blur' }],
        custPost: [{ required: true, message: '请输入', trigger: 'blur' }],
        telephone: [{ required: true, validator: checkPhone, trigger: 'blur' }],
        ourContacts: [{ required: true, message: '请输入', trigger: 'blur' }],
        businessScope: [{ required: true, message: '请输入', trigger: 'blur' }],
        enterpriseProfile: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    };
  },
  computed: {
    isEdit () {
      return this.EcologicalId != null && this.EcologicalId !== '';
    }
  },
  mounted () {
    this.loadareaCodelistDict()
    if (this.isEdit) {
      this.loadknowledgedetail()
    }
  },
  methods: {
    /**
     * 知识库详情
     */
    loadknowledgedetail () {
      let params = this.EcologicalId
      return new Promise((resolve, reject) => {
        EcologicalkKnowledge.detail(this, params).then(res => {
          this.ruleForm = res.data
          this.ruleForm.unitAttribute = res.data.unitAttribute.toString()
          this.ruleForm.districtCode = Number(this.ruleForm.districtCode)
        }).catch(e => {
          reject(e);
        });
      });
    },
    /**
     * 行政区划
     */
    loadareaCodelistDict () {
      return new Promise((resolve, reject) => {
        EcologicalkKnowledge.listDict(this).then(res => {
          let areaCodelist = res.data
          let areaCodelistnew = []
          areaCodelist.forEach((item, index) => {
            if (!item.parentId) {
              item.children = []
              areaCodelistnew.push(item)
            }
          })
          areaCodelistnew.forEach((item1, index1) => {
            let info = areaCodelist.find((item2, index2) => {
              return item1.id === item2.parentId
            })
            if (info) {
              item1.children = []
              areaCodelist.forEach((item3, index3) => {
                if (item1.id === item3.parentId) {
                  item1.children.push(item3)
                }
              })
            } else {
              item1.children = null
            }
          })
          areaCodelistnew.forEach((item4, index4) => {
            if (item4.children) {
              item4.children.forEach((item5, index5) => {
                let info2 = areaCodelist.find((item6, index6) => {
                  return item5.id === item6.parentId
                })
                if (info2) {
                  item5.children = []
                  areaCodelist.forEach((item7, index7) => {
                    if (item5.id === item7.parentId) {
                      item5.children.push(item7)
                    }
                  })
                } else {
                  item5.children = null
                }
              })
            }
          })
          this.areaCodelist = areaCodelistnew
        }).catch(e => {
          reject(e);
        });
      });
    },
    /**
     * 选择行政区划
     */
    areaCodesChange () {
      this.$refs['ref_areaCodes'].dropDownVisible = false
    },
    /**
     * 提交表单
     */
    submitForm () {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          this.isShowloading = true;
          let params = this.ruleForm;
          if (this.EcologicalId) { // 修改
            return new Promise((resolve, reject) => {
              EcologicalkKnowledge.update(this, params).then(res => {
                this.$message({
                  showClose: true,
                  message: '修改成功！',
                  type: 'success'
                });
                this.onCancel(true);
              }).catch(e => {
                reject(e);
              });
            });
          } else { // 添加
            return new Promise((resolve, reject) => {
              EcologicalkKnowledge.add(this, params).then(res => {
                this.$message({
                  showClose: true,
                  message: '添加成功！',
                  type: 'success'
                });
                this.onCancel(true);
              }).catch(e => {
                reject(e);
              });
            });
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    onCancel (isSuccess) {
      if (this.observer != null) {
        this.observer.cancel(isSuccess);
      }
    }
  }
}
</script>

<style scoped>
h3{border-bottom: 1px solid #E9E9EB;padding: 0 0 10px;}
.table_container{margin: 10px 0;}
.form-single-info{display: flex;justify-content: center;}
.el-form-info{box-shadow: 0 0 10px rgba(0,0,0,0.05);padding: 20px;}
.el-form-info strong{margin: 0 10px;}
.el-form-info .el-time-picker{width: 100%;}
</style>
